package com.turing.manage.email.user;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.turing.manage.email.utils.CodeUtils;
import com.turing.manage.email.utils.EmailUtils;



/**
 * @desc   User模块的C层
 * @author wyh
 * @time   2022-01-06
 */
@SuppressWarnings("serial")
public class UserServlet  extends HttpServlet{

UserService  userService=new UserService();	
	
	
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	String method=request.getParameter("method");
	
	try {
		if ("register".equals(method))
		{
			this.register(request,response);
		}
		else if ("validation".equals(method))
		{
			this.validation(request,response);
		}
		else if ("query".equals(method))
		{
			this.query(request,response);
		}
		else if ("sendImage".equals(method))
		{
			this.sendImage(request,response);
		}
		else if ("sendFile".equals(method))
		{
			this.sendFile(request,response);
		}
	} 
	catch (MessagingException | ClassNotFoundException | SQLException e) 
	{
		System.out.println("邮件发送异常，且异常信息为:"+e.getMessage());
	}
	
	
	
}

/**
 * @desc  发送带有附件的邮件
 * @param request
 * @param response
 * @throws MessagingException 
 * @throws IOException 
 */
private void sendFile(HttpServletRequest request, HttpServletResponse response) throws MessagingException, IOException {
	
	//1.接值
			String receiver=request.getParameter("receiver");
			String emailText=request.getParameter("emailText");
			String fileUrl=request.getParameter("fileUrl");
			String emailSubject=request.getParameter("emailSubject");
			System.out.println(" receiver:"+receiver);
			System.out.println(" emailText:"+emailText);
			System.out.println(" fileUrl:"+fileUrl);
			System.out.println(" emailSubject:"+emailSubject);	
			
            //2.调用EmailUtils工具类中的方法实现发送带有附件的邮件
			EmailUtils.createMineMessage2(receiver, emailSubject, emailText, fileUrl);
	
			//重定向
			response.sendRedirect(request.getContextPath()+"/");
}

/**
 * @desc  发送带有图片的邮件
 * @param request
 * @param response
 * @throws MessagingException 
 * @throws IOException 
 */
private void sendImage(HttpServletRequest request, HttpServletResponse response) throws MessagingException, IOException {
	
	String receiver=request.getParameter("receiver");
	String emailText=request.getParameter("emailText");
	String imageUrl=request.getParameter("imageUrl");
	String emailSubject=request.getParameter("emailSubject");
	System.out.println(" receiver:"+receiver);
	System.out.println(" emailText:"+emailText);
	System.out.println(" imageUrl:"+imageUrl);
	System.out.println(" emailSubject:"+emailSubject);

	 //发送带有图片的邮件
	EmailUtils.createMineMessage(receiver,emailSubject,emailText, imageUrl);
	
	//重定向
	response.sendRedirect(request.getContextPath()+"/");
}

/**
 * @desc  查询用户信息
 * @param request
 * @param response
 * @throws IOException 
 * @throws ServletException 
 * @throws SQLException 
 * @throws ClassNotFoundException 
 */
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {
	
	List<Map<String, Object>>list_user=userService.queryAll();
	request.setAttribute("list_user", list_user);
    request.getRequestDispatcher("/manage/user/list.jsp").forward(request, response);	
	
	
}

/**
 * @desc  校验激活
 * @param request
 * @param response
 * @throws IOException 
 * @throws SQLException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 * @throws ServletException 
 */
private void validation(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, FileNotFoundException, SQLException, IOException, ServletException {
	//1.接值：接收校验页面用户填写的验证码
	String ucode=request.getParameter("ucode");
	
	//2.调用M层中的方法执行激活
	Map<String, Object> map=userService.queryUcode(ucode);
	
	if (map==null) 
	{
	  String errorMsg="验证码错误";
	  request.setAttribute("errorMsg", errorMsg);
      request.getRequestDispatcher("/manage/user/error.jsp").forward(request, response);		
	}
	else
	{
	 userService.updateStatues(ucode);	
	 response.sendRedirect(request.getContextPath()+"/email.do?method=query");	
	}
	
}


/**
 * @desc  新用户注册方法
 * @param request
 * @param response
 * @throws MessagingException 
 * @throws IOException 
 * @throws SQLException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 * @throws ServletException 
 */
private void register(HttpServletRequest request, HttpServletResponse response) throws MessagingException, ClassNotFoundException, FileNotFoundException, SQLException, IOException, ServletException {
	
	//1.接值：接user_add.jsp页面传递过来的新注册用户的信息
	String  username=request.getParameter("username");
	String  password=request.getParameter("password");
	String  email=request.getParameter("email");
    String  ucode=CodeUtils.createCode();//调用注册码工具类产生注册码
    
   //// System.out.println("username:"+username);
    //System.out.println("password:"+password);
   // System.out.println("email:"+email);
   // System.out.println("ucode:"+ucode);
   
   //2.保存新用户的信息，并发送邮件
    EmailUtils.createMineMessage(request, email, ucode);
    userService.save(username,password,email,ucode);
	
  //3.将注册成功的信息填入到request作用域中
  		request.setAttribute("message", "恭喜您，注册成功，我们已经发了一封带了注册信息的电子邮件，请查收，如果没有收到，可能是网络原因，过一会儿就收到了！！");
    
  //4.转向
    request.getRequestDispatcher("/manage/user/success.jsp").forward(request, response);
}

	
	
	
	
	
}
